package io.file;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author lcx
 *
 * 采用两种方法复制文件
 * 
 */
public class FileCopy {
	
	
	/**
	 * 
	 * 用Stream的办法进行文件复制
	 * @throws IOException
	 */
	public static void fileCopy1()throws IOException 
	{
		FileInputStream a=new FileInputStream("a.txt");
		FileInputStream b=new FileInputStream("b.txt");
		FileOutputStream c=new FileOutputStream("c.txt");
		byte[] bt=new byte[5];
		int tag=0;
		while( (tag=a.read(bt))>0)
		{
			c.write(bt);
		}
		
		while( (tag=b.read(bt))>0)
		{
			c.write(bt);
		}
		a.close();
		b.close();
		c.close();
	}
	
	
	/**
	 * 
	 * 利用reader/writer方法进行文件复制
	 * 但是如果流不关闭，则无法查看修改
	 * @throws IOException
	 */
	public static void fileCopy2()throws IOException 
	{
		FileReader a=new FileReader("a.txt");
		FileReader b=new FileReader("b.txt");
		FileWriter c=new FileWriter("c.txt");
		char[] bt=new char[5];
		int tag=0;
		while((tag=a.read(bt))>0)
		{
			c.write(bt);
		}
		
		while((tag=b.read(bt))>0)
		{
			c.write(bt);
		}
		
		a.close();
		b.close();
		c.close();
	}
	
	
	/**
	 * 
	 * 利用reader/writer方法进行文件复制
	 * 但是如果流不关闭，则无法查看修改
	 * @throws IOException
	 */
	public static void fileCopy3()throws IOException 
	{
		FileReader a=new FileReader("a.txt");
		FileReader b=new FileReader("b.txt");
		FileWriter c=new FileWriter("c.txt");
		char[] bt=new char[5];
		BufferedReader buffReadera=new BufferedReader(a);
		BufferedReader buffReaderb=new BufferedReader(b);
		String str="";
		while((str=buffReadera.readLine())!=null)
		{
			System.out.println("   "+str);
			c.write(str, 0, str.length());
		}
		
		while((str=buffReaderb.readLine())!=null)
		{
			System.out.println("   "+str);
			c.write(str, 0, str.length());
		}
		
		a.close();
		b.close();
		c.close();
	}
	
	public static void main(String[] args) throws IOException {
//		FileCopy.fileCopy1();
		FileCopy.fileCopy2();
	}
}
